package fengcan;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.sql.DataSource;

import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.StaticLoggerBinder;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import Entity.WcsDvcEntity;
import log.WcsLog;
import lombok.SneakyThrows;
import utils.DbUnits;
import utils.GlobalOjbectUnits;
import utils.LogUnits;
import window.WcsWindow;

public class RunApp {
	private static WcsLog logger;
	
	
	public static String dvcTp;//根据程序名称匹配数据中的plc设备信息
	

	public static void main(String[] args) throws Exception {
		WcsWindow window = new WcsWindow();
		GlobalOjbectUnits.setWindow(window);//将可视化实例对象保存为静态对象，给多个类使用。
		logger = LogUnits.getLogger(RunApp.class);
		dvcTp = "wcs-test";
		dvcTp = getOwnName();
		window.setTitle(dvcTp);

//		LogUnits.infoMoreLog("正在检测待连接设备。。。。。");
		logger.info("程序名称： "+dvcTp);
		logger.info("正在查找待连接设备。。。。。");
//		List<WcsDvcEntity> wcsDvcList = getWcsDvcEntityList();
//		LogUnits.infoMoreLog("程序："+dvcTp+",总共查找到："+wcsDvcList.size()+"个待连接设备！！");
//		logger.info("程序："+dvcTp+",总共查找到："+wcsDvcList.size()+"个待连接设备！！");
//		for(WcsDvcEntity wcs : wcsDvcList){
//			//该线程是为了建立多个ip:port的socket连接
//			new SocketThread(wcs).start();
//		}

	}
	/**
	 * 从数据库中查找ip、port、procedure信息
	 * @return
	 */
	public static List<WcsDvcEntity> getWcsDvcEntityList() {
		final String IS_OK = "1";
		
		List<WcsDvcEntity> wcsDvcList = new ArrayList<>();
		Connection conn = null;
		try {
			conn = DbUnits.getConnection();
			String sql = "select * from wcs_dvc where isok=? and dvc_tp=?";
			PreparedStatement preparedStatement = conn.prepareStatement(sql);
			preparedStatement.setString(1, IS_OK);
			preparedStatement.setString(2, dvcTp);
			ResultSet rs = preparedStatement.executeQuery();
			while(rs.next()){
				WcsDvcEntity wcs = new WcsDvcEntity();
				wcs.setDvcId(rs.getInt("dvc_id"));
				wcs.setDvcNo(rs.getString("dvc_no"));
				wcs.setDvcDesc(rs.getString("dvc_desc"));
				wcs.setDvcTp(rs.getString("dvc_tp"));
				wcs.setDvcIp(rs.getString("dvc_ip"));
				wcs.setPort(rs.getString("port"));
				wcs.setRevBegin(rs.getString("rev_begin"));
				wcs.setRevEnd(rs.getString("rev_end"));
				wcs.setRevProc(rs.getString("rev_proc"));
				wcs.setSendProc(rs.getString("send_proc"));
				wcs.setSendTime(rs.getInt("send_time"));
				wcs.setIsok(rs.getString("isok"));
				wcsDvcList.add(wcs);
			}
			
 
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取ip:port、procedure信息失败！！");
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("connection连接关闭失败！");
				e.printStackTrace();
			}
		}
		
		return wcsDvcList;
	}
	
	public static String getOwnName() {
		//如下代码获得：D:\java\workspace\produce\target\produce-0.0.1-SNAPSHOT-jar-with-dependencies.jar
		String path2 = System.getProperty("java.class.path");
		logger.info(path2);
		//如下代码获得： /D:/java/workspace/produce/target/produce-0.0.1-SNAPSHOT-jar-with-dependencies.jar
		String path = RunApp.class.getProtectionDomain().getCodeSource().getLocation().getFile();
		logger.info(path);
		String ownName = null;
		if (path.endsWith(".jar")) {
			ownName = path.substring(path.lastIndexOf("/") + 1,
					path.length() - 4);
		}else {
			ownName = "程序非jar类型";
		}
		return ownName;
	}
	
}
